package service.impl.v2;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.alibaba.druid.sql.visitor.functions.If;

import dao.v2.AddressVOMapper;
import dao.v2.ConfigVOMapper;
import dao.v2.GoodVOMapper;
import dao.v2.MemberLevelItemVOMapper;
import dao.v2.MemberVOMapper;
import dao.v2.OrderVOMapper;
import dao.v2.PromotionVOMapper;
import model.AddressVO;
import model.BoardVO;
import model.GoodDeliveryInterCountryVO;
import model.GoodStandardVO;
import model.GoodVO;
import model.MemberVO;
import model.OrderEvaluateVO;
import model.OrderGoodVO;
import model.PromotionItemVO;
import model.PromotionVO;
import model.SendTempItemVO;
import model.StandardVO;
import service.v2.AddressService;
import service.v2.GoodService;
import service.v2.GoodTempService;
import service.v2.MaterialService;
import service.v2.MemberCartServise;
import service.v2.OrderService;
import service.v2.PromotionService;
import service.v2.TimerService;
import util.DateParseUtil;
import util.ListSortUtil;
import util.NumberUtil;
import util.PackageUtil;
import util.PropertieUtil;
import util.StringUtil;
@Service
public class GoodServiceImpl implements GoodService {
	
	@Autowired
	private GoodVOMapper goodVOMapper;
	@Autowired
	private ConfigVOMapper  configDao;
	@Autowired
	private MaterialService  materialService;
	@Autowired
	private OrderService  orderService;
	@Autowired
	private OrderVOMapper  orderVOMapper;
	@Autowired
	private TimerService  timerService;
	@Autowired
	private MemberLevelItemVOMapper memberLevelItemDao;
	@Autowired
	private PromotionVOMapper promotionDao;
	@Autowired
	private MemberVOMapper memberDao;
	@Autowired
	private GoodTempService goodTempService;
	@Autowired
	private MemberCartServise memberCartServise;
	@Autowired
	private AddressService addressService;
	@Autowired
	private AddressVOMapper addressDao;
	
	public List<GoodVO> getHotList(GoodVO goodVO) {
		return goodVOMapper.getHotList(goodVO);
	}
	
	@Cacheable(value="GoodServiceImpl_getGoodById",key="#root.targetClass+#root.methodName+#id")
	public GoodVO getGoodById(String id, String file_cat) {
		return goodVOMapper.getGoodById(id,file_cat);
	}

	@Cacheable(value="GoodServiceImpl_getTotal",key="#root.targetClass+#root.methodName+#goodVO.lang+#goodVO.search_str+#goodVO.cat_id+#goodVO.top_cat_id+#goodVO.third_cat_id+#goodVO.promotion_id+#goodVO.brand_id")
	public int getTotal(GoodVO goodVO) {
		return goodVOMapper.getTotal(goodVO);
	}
	
	@Cacheable(value="GoodServiceImpl_getGoodList",key="#root.targetClass+#root.methodName+#goodVO.lang+#goodVO.search_str+#goodVO.cat_id+#goodVO.top_cat_id+#goodVO.third_cat_id+#goodVO.promotion_id+#goodVO.ord+#goodVO.page_index+#goodVO.page_count+#goodVO.brand_id")
	public List<GoodVO> getGoodList(GoodVO goodVO) {
		return  goodVOMapper.getGoodList(goodVO);
	}

	public int checkGoodStandardById(String id) {
		return goodVOMapper.checkGoodStandardById(id);
	}
	/*商品详细 图文详细*/	
	public GoodVO getGoodDetilById(String good_id) {
		return goodVOMapper.getGoodDetilById(good_id);
	}

	@Cacheable(value="GoodServiceImpl_getSpecialTotal",key="#root.targetClass+#root.methodName+#goodVO.cat_id+#goodVO.lang+#goodVO.package_yn")
	public int getSpecialTotal(GoodVO goodVO) {
		return goodVOMapper.getSpecialTotal(goodVO);
	}
	
	@Cacheable(value="GoodServiceImpl_getSpecialGoodList",key="#root.targetClass+#root.methodName+#goodVO.lang+#goodVO.package_yn+#goodVO.cat_id+#goodVO.page_yn+#goodVO.page_index+#goodVO.page_count")
	public List<GoodVO> getSpecialGoodList(GoodVO goodVO) {
		return goodVOMapper.getSpecialGoodList(goodVO);
	}

	public OrderEvaluateVO getOneEvaluByGoodId(String good_id) {
		return goodVOMapper.getOneEvaluByGoodId(good_id);
	}

	@Override
	public String processDeductionInfo(String buy_type, String ticket, String ticket_price, MemberVO memberVO,
			String ticket_name, String country) {
		String deduction_info = "";
		try{
			if(memberVO!=null && memberVO.getLevel() != null && !PropertieUtil.loadPropertie("common_level").equals(memberVO.getLevel())){
				String rate = memberLevelItemDao.getRateByLevel(memberVO.getLevel());
				if(PropertieUtil.loadPropertie("buy_type_cash").equals(buy_type)){
					deduction_info="";
				}else if(PropertieUtil.loadPropertie("buy_type_ticket").equals(buy_type) || PropertieUtil.loadPropertie("buy_type_cash_ticket").equals(buy_type)){
					if("1".equals(country)){
						deduction_info = "使用"+ticket +ticket_name + "可抵[" + NumberUtil.formatDouble(Integer.parseInt(ticket) * Double.parseDouble(rate), 2) + "]元";
					}else if("0".equals(country)){
						deduction_info = ticket + " Uni Tickets can be used to redeem [" + NumberUtil.formatDouble(Integer.parseInt(ticket) * Double.parseDouble(rate), 2) + "$]";
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return deduction_info;
	}
	
	public String processPrivilegePrice(String buy_type,String ticket,String ticket_price, MemberVO memberVO, String ticket_name) {
		String privilege_price = "";
		try{
			if(memberVO!=null && memberVO.getLevel() != null  && !PropertieUtil.loadPropertie("common_level").equals(memberVO.getLevel())){
				if(PropertieUtil.loadPropertie("buy_type_cash").equals(buy_type)){
					privilege_price="";
				}else if(PropertieUtil.loadPropertie("buy_type_ticket").equals(buy_type)){
					privilege_price = ticket +ticket_name;
				}else if(PropertieUtil.loadPropertie("buy_type_cash_ticket").equals(buy_type)){
					privilege_price=ticket_price.replace(".00", "") +"元+"+ticket+ticket_name;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return privilege_price;
	}
	
	@Override
	public void processPromotionList(List<GoodVO> list, MemberVO memberVO) throws Exception {
		if(list != null && list.size() != 0){
			for (GoodVO goodVO : list) {
				//促销信息
				String promotion_item = processPromotionItem(goodVO.getPromotionItemVO(), memberVO);
				goodVO.setPromotion_item(promotion_item);
			}
		}
	}
	
	public List<GoodVO> processGoodList(List<GoodVO> list, MemberVO memberVO, String ticket_name) throws Exception {
		if(list != null && list.size() != 0){
			for (GoodVO goodVO : list) {
				//优惠购
				String privilege_price = processPrivilegePrice(goodVO.getBuy_type(), goodVO.getTicket(), goodVO.getTicket_price(), memberVO, ticket_name);
				goodVO.setPrivilege_price(privilege_price);
				//会员价
				String member_price = processMemberPrice(goodVO.getDefault_price(), goodVO.getBuy_type(), goodVO.getTicket(), goodVO.getTicket_price(), memberVO);
				goodVO.setMember_price(member_price);
				//促销信息
				String promotion_item = processPromotionItem(goodVO.getPromotionItemVO(), memberVO);
				goodVO.setPromotion_item(promotion_item);
			}
		}
		return list;
	}
	
	public String processPromotionItem(PromotionItemVO promotionItemVO, MemberVO memberVO) throws Exception {
		String promotion_item = "";
		if(promotionItemVO != null){
			PromotionVO promotionVO = promotionDao.getPromotionById(promotionItemVO.getPromotion_id());
			boolean flag = promotionShow(memberVO, promotionVO.getMax_level());
			if(flag){
				promotion_item = promotionItem(promotionItemVO, promotionVO);
			}
		}
		return promotion_item;
	}

	public String promotionItem(PromotionItemVO promotionItemVO, PromotionVO promotionVO) {
		String promotion_item;
		promotion_item = "满" + promotionItemVO.getReach() + ("0".equals(promotionVO.getReach_type())?"":"件");
		if(promotionItemVO.getReduce_cash() != null && Double.parseDouble(promotionItemVO.getReduce_cash()) > 0){
			promotion_item += "减" + promotionItemVO.getReduce_cash();
		}
		if("Y".equals(promotionItemVO.getPromotion_freight())){
			promotion_item += "包邮";
		}
		return promotion_item;
	}
	@Override
	public String promotionItemEn(PromotionItemVO promotionItemVO, PromotionVO promotionVO) {
		String promotion_item="";
		//满元减
		if("0".equals(promotionVO.getReach_type())){
			promotion_item = "Get US $"+promotionItemVO.getReduce_cash()+" off on orders over US $"+ promotionItemVO.getReach();
		}
		//满件件
		if("1".equals(promotionVO.getReach_type())){
			promotion_item = "Get $"+promotionItemVO.getReduce_cash()+" off on orders over $"+ promotionItemVO.getReach()+"piece(s)";

		}
		return promotion_item;
		
		
	}
	public boolean promotionShow(MemberVO memberVO, String max_level) throws Exception {
		boolean flag = true;
		//没登陆全部可以看
		if(memberVO!=null && memberVO.getLevel() != null){
			String level = memberVO.getLevel();
			int num = memberLevelItemDao.compareLevel(level, max_level);
			if(num > 0){
				flag = false;
			}
		}
		return flag;
	}

	public String processMemberPrice(String sell_price, String buy_type, String ticket, String ticket_price, MemberVO memberVO) throws Exception {
		String member_price = "";
		
		String ticket_rule = configDao.loadConfig("MEMBER_PRICE_TICKET");
		String percent_rule = configDao.loadConfig("MEMBER_PRICE_PERCENT");
		// 满足条件
		if (Integer.parseInt(ticket) >= Integer.parseInt(ticket_rule) || Integer.parseInt(ticket)
				/ (Double.parseDouble(sell_price) * 100) >= Double.parseDouble(percent_rule)) {
			String rate = null;
			if (PropertieUtil.loadPropertie("common_level").equals(memberVO.getLevel()) || memberVO.getLevel() == null) {
				rate = memberLevelItemDao.getRateByLevel(PropertieUtil.loadPropertie("expert_level"));
			} else {
				rate = memberLevelItemDao.getRateByLevel(memberVO.getLevel());
			}
			if (PropertieUtil.loadPropertie("buy_type_cash").equals(buy_type)) {
				member_price = "";
			} else if (PropertieUtil.loadPropertie("buy_type_ticket").equals(buy_type)) {
				member_price = NumberUtil.formatDouble(Integer.parseInt(ticket) * Double.parseDouble(rate), 2);
			} else if (PropertieUtil.loadPropertie("buy_type_cash_ticket").equals(buy_type)) {
				member_price = NumberUtil.formatDouble(
						(Double.parseDouble(ticket_price) + (Integer.parseInt(ticket) * Double.parseDouble(rate))), 2);
			}
		}
		return member_price;
	}
	
	@Cacheable(value="GoodServiceImpl_getRandomGoodListNoLogin",key="#root.targetClass+#root.methodName+#goodVO.lang+#goodVO.page_index")
	public List<GoodVO> getRandomGoodListNoLogin(GoodVO goodVO) {
		return goodVOMapper.getRandomGoodListNoLogin(goodVO);
	}
	
	public List<GoodVO> getRandomGoodListLogin(String member_id,String member_recommend) {
		return goodVOMapper.getRandomGoodListLogin(member_id,member_recommend);
	}
	public void processImg(List<GoodVO> list, String screenwidth, int i) {
		if(list != null && list.size() != 0){
			for (GoodVO goodVO : list) {
				if(!"".equals(goodVO.getImg())){
					goodVO.setImg(goodVO.getImg()+"?x-oss-process=image/resize,w_"+Integer.parseInt(screenwidth)/i);
				}
			}
		}
	}

	public String getMemberRecommend(String member_id, String num) {
		return goodVOMapper.getMemberRecommend(member_id, num);
	}
	
	public int checkGoodById(String good_id) {
		return goodVOMapper.checkGoodById( good_id) ;
	}

	public String getGoodPackageName(String good_id) {
		
		List list = goodVOMapper.getGoodPackageName(good_id);
		
		return list.size()>0?(String) list.get(0):"";
	}

	public List<GoodVO> setShareMsg(List<GoodVO> list,String invite_no) {
		
		GoodVO vo1 =new GoodVO();
		for(int i=0; i<list.size();i++){
			GoodVO vo =new GoodVO();
			list.get(i).setShare(vo);
			try {
				vo1 = materialService.processShareInfo(list.get(i));
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(list.get(i).getImg().indexOf("?")>-1){
				String arr[]=list.get(i).getImg().split("?");
				list.get(i).getShare().setImg(arr[0]+"?x-oss-process=image/resize,h_100");
			}else{
				list.get(i).getShare().setImg(list.get(i).getImg()+"?x-oss-process=image/resize,h_100");
			}
			list.get(i).getShare().setUrl(configDao.loadConfig("WECHAT_HOST")+"/goodDetail?good_id="+list.get(i).getGood_id()+"&ym_share_inviteno="+invite_no);
			list.get(i).getShare().setTitle(list.get(i).getName());
			list.get(i).getShare().setContent(configDao.loadConfig("SHARE_GOOD_DETAIL_CONTENT"));//亲爱的，这件商品友米乐有卖哦，正品低价，推荐给你。
			list.get(i).getShare().setMin_share_info(vo1.getStandard_list().get(0).getMin_share_info());
			list.get(i).getShare().setShare_info(vo1.getStandard_list().get(0).getShare_info());
			
		}
		return list;
	}
	
	public GoodVO getGoodInfoById(String good_id, String folder_cat) {
		return goodVOMapper.getGoodInfoById(good_id, folder_cat);
	}


	public Object dealNewStadardDetails(String good_id, String type_id, String type_child_name, String searchState,String last_type_id,String last_type_child_name,MemberVO memberVO) {
		// TODO Auto-generated method stub
		String[] type_id_list = type_id.split(",");
		String[] type_child_name_list = type_child_name.split(",");
		
		String type_id1 = "";
		String type_id2 = "";
		String type_id3 = "";
		String type_child_name1="";
		String type_child_name2="";
		String type_child_name3="";
		if(type_id_list.length==1){
			type_id1 = type_id_list[0];
			type_child_name1 = type_child_name_list[0];
		}else if(type_id_list.length==2){
			type_id1 = type_id_list[0];
			type_child_name1 = type_child_name_list[0];
			type_id2 = type_id_list[1];
			type_child_name2 = type_child_name_list[1];
		}else if(type_id_list.length==3){
			type_id1 = type_id_list[0];
			type_child_name1 = type_child_name_list[0];
			type_id2 = type_id_list[1];
			type_child_name2 = type_child_name_list[1];
			type_id3 = type_id_list[2];
			type_child_name3 = type_child_name_list[2];
		}
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("good_id", good_id);
		map.put("type_id1", type_id1);
		map.put("type_id2", type_id2);
		map.put("type_id3", type_id3);
		map.put("type_child_name1", type_child_name1);
		map.put("type_child_name2", type_child_name2);
		map.put("type_child_name3", type_child_name3);
		map.put("count", type_id_list.length);
		
		//部分选
		if("0".equals(searchState)){
			if(type_id_list.length==1){
				List<StandardVO> standardList = new ArrayList<StandardVO>();
				StandardVO standardType =  goodVOMapper.getOneType(map);
				List<StandardVO> chidList =  goodVOMapper.getPartOfDetails(map);
				StandardVO retObj = new StandardVO();
				retObj.setChildList(chidList);
				retObj.setType_id(standardType.getType_id());
				retObj.setType_name(standardType.getType_name());
				retObj.setGood_id(standardType.getGood_id());
				
				StandardVO standardType1 =  goodVOMapper.getOneType1(map);
				List<StandardVO> chidList1 =  goodVOMapper.getPartOfDetails1(map);
				StandardVO retObj1 = new StandardVO();
				if(standardType1!=null){
					retObj1.setChildList(chidList1);
					retObj1.setType_id(standardType1.getType_id());
					retObj1.setType_name(standardType1.getType_name());
					retObj1.setGood_id(standardType1.getGood_id());
					standardList.add(retObj1);
				}
				
				standardList.add(retObj);
				
				return standardList;
			}else if(type_id_list.length==2){
				List<StandardVO> standardList = new ArrayList<StandardVO>();
				StandardVO standardType =  goodVOMapper.getOneType(map);
				List<StandardVO> chidList =  goodVOMapper.getPartOfDetails(map);
				StandardVO retObj = new StandardVO();
				retObj.setChildList(chidList);
				retObj.setType_id(standardType.getType_id());
				retObj.setType_name(standardType.getType_name());
				retObj.setGood_id(standardType.getGood_id());
				standardList.add(retObj);
				return standardList;
			}
			
			
		}
		//全选
		else if("1".equals(searchState)){
			//会员折扣率
			double member_rate = 1;
			if(memberVO!=null&&memberVO.getId()!=null){
				member_rate = orderVOMapper.getDepositRate(memberVO.getId());
			}

			if(type_id_list.length==1){
				StandardVO standardvo = goodVOMapper.getStandardIdNum(map);
				//获取优惠购以及分享赚
				Map prishareMap = getStandardPriAndShare(standardvo.getStandard_id(),memberVO);
				standardvo.setPrivilege_price(prishareMap.get("privilege_price")==null?"":prishareMap.get("privilege_price").toString());
				standardvo.setShare_info(prishareMap.get("share_info")==null?"":prishareMap.get("share_info").toString());
				standardvo.setMin_share_info(prishareMap.get("min_share_info")==null?"":prishareMap.get("min_share_info").toString());
				if(memberVO!=null){
					String ticket_rule = configDao.loadConfig("MEMBER_PRICE_TICKET");
					String percent_rule = configDao.loadConfig("MEMBER_PRICE_PERCENT");
					//满足条件
					if(Integer.parseInt(standardvo.getTicket()) >= Integer.parseInt(ticket_rule) || Integer.parseInt(standardvo.getTicket()) / (Double.parseDouble(standardvo.getSell_price()) * 100) >= Double.parseDouble(percent_rule)){
						if(1 == member_rate){
							standardvo.setMember_price(NumberUtil.formatDouble(Double.parseDouble(standardvo.getTicket_price())+Integer.parseInt(standardvo.getTicket())*0.55,2));
							standardvo.setReduce_money(NumberUtil.formatDouble(Integer.parseInt(standardvo.getTicket())*0.55, 2));	
						}else{
							standardvo.setMember_price(NumberUtil.formatDouble(Double.parseDouble(standardvo.getTicket_price())+Integer.parseInt(standardvo.getTicket())*member_rate,2));
							standardvo.setReduce_money(NumberUtil.formatDouble(Integer.parseInt(standardvo.getTicket())*member_rate, 2));	
						}
					}else{
						standardvo.setMember_price("");
						standardvo.setReduce_money("");
					}
				}else{
					String ticket_rule = configDao.loadConfig("MEMBER_PRICE_TICKET");
					String percent_rule = configDao.loadConfig("MEMBER_PRICE_PERCENT");
					if(Integer.parseInt(standardvo.getTicket()) >= Integer.parseInt(ticket_rule) || Integer.parseInt(standardvo.getTicket()) / (Double.parseDouble(standardvo.getSell_price()) * 100) >= Double.parseDouble(percent_rule)){
						standardvo.setMember_price(NumberUtil.formatDouble(Double.parseDouble(standardvo.getTicket_price())+Integer.parseInt(standardvo.getTicket())*0.55,2));
						standardvo.setReduce_money(NumberUtil.formatDouble(Integer.parseInt(standardvo.getTicket())*0.55, 2));		
					}else{
						standardvo.setMember_price("");
						standardvo.setReduce_money("");
					}

				}
				
				List<StandardVO> standardList = new ArrayList<StandardVO>();
				StandardVO standardType =  new StandardVO();
				List<StandardVO> chidList =  new ArrayList<StandardVO>();
				StandardVO retObj = new StandardVO();
				retObj.setChildList(chidList);
				retObj.setType_id("");
				retObj.setType_name("");
				retObj.setGood_id(standardType.getGood_id());
				retObj.setStandard_msg(standardvo);
				standardList.add(retObj);

				return standardList;
				
			}
			//2阶
			else if(type_id_list.length==2){
				StandardVO standardvo = goodVOMapper.getStandardIdNum(map);
				Map prishareMap = getStandardPriAndShare(standardvo.getStandard_id(),memberVO);
				standardvo.setPrivilege_price(prishareMap.get("privilege_price")==null?"":prishareMap.get("privilege_price").toString());
				standardvo.setShare_info(prishareMap.get("share_info")==null?"":prishareMap.get("share_info").toString());
				standardvo.setMin_share_info(prishareMap.get("min_share_info")==null?"":prishareMap.get("min_share_info").toString());
				if(memberVO!=null){
					String ticket_rule = configDao.loadConfig("MEMBER_PRICE_TICKET");
					String percent_rule = configDao.loadConfig("MEMBER_PRICE_PERCENT");
					//满足条件
					if(Integer.parseInt(standardvo.getTicket()) >= Integer.parseInt(ticket_rule) || Integer.parseInt(standardvo.getTicket()) / (Double.parseDouble(standardvo.getSell_price()) * 100) >= Double.parseDouble(percent_rule)){
						if(1 == member_rate){
							standardvo.setMember_price(NumberUtil.formatDouble(Double.parseDouble(standardvo.getTicket_price())+Integer.parseInt(standardvo.getTicket())*0.55,2));
							standardvo.setReduce_money(NumberUtil.formatDouble(Integer.parseInt(standardvo.getTicket())*0.55, 2));	
						}else{
							standardvo.setMember_price(NumberUtil.formatDouble(Double.parseDouble(standardvo.getTicket_price())+Integer.parseInt(standardvo.getTicket())*member_rate,2));
							standardvo.setReduce_money(NumberUtil.formatDouble(Integer.parseInt(standardvo.getTicket())*member_rate, 2));	
						}
					}else{
						standardvo.setMember_price("");
						standardvo.setReduce_money("");
					}
				}else{
					String ticket_rule = configDao.loadConfig("MEMBER_PRICE_TICKET");
					String percent_rule = configDao.loadConfig("MEMBER_PRICE_PERCENT");
					if(Integer.parseInt(standardvo.getTicket()) >= Integer.parseInt(ticket_rule) || Integer.parseInt(standardvo.getTicket()) / (Double.parseDouble(standardvo.getSell_price()) * 100) >= Double.parseDouble(percent_rule)){
						standardvo.setMember_price(NumberUtil.formatDouble(Double.parseDouble(standardvo.getTicket_price())+Integer.parseInt(standardvo.getTicket())*0.55,2));
						standardvo.setReduce_money(NumberUtil.formatDouble(Integer.parseInt(standardvo.getTicket())*0.55, 2));		
					}else{
						standardvo.setMember_price("");
						standardvo.setReduce_money("");
					}
				}
				
				Map<String,Object> map2 = new HashMap<String,Object>();  
				map2.put("good_id", good_id);
				map2.put("type_id1", last_type_id);
				map2.put("type_child_name1", last_type_child_name);
				map2.put("count", 1);
				
				List<StandardVO> standardList = new ArrayList<StandardVO>();
				StandardVO standardType =  goodVOMapper.getOneType(map2);
				List<StandardVO> chidList =  goodVOMapper.getPartOfDetails(map2);
				StandardVO retObj = new StandardVO();
				retObj.setChildList(chidList);
				retObj.setType_id(standardType.getType_id());
				retObj.setType_name(standardType.getType_name());
				retObj.setGood_id(standardType.getGood_id());
				retObj.setStandard_msg(standardvo);
				standardList.add(retObj);

				return standardList;
			}
			//3阶
			else if(type_id_list.length==3){
				StandardVO standardvo = goodVOMapper.getStandardIdNum(map);
				Map prishareMap = getStandardPriAndShare(standardvo.getStandard_id(),memberVO);
				standardvo.setPrivilege_price(prishareMap.get("privilege_price")==null?"":prishareMap.get("privilege_price").toString());
				standardvo.setShare_info(prishareMap.get("share_info")==null?"":prishareMap.get("share_info").toString());
				standardvo.setMin_share_info(prishareMap.get("min_share_info")==null?"":prishareMap.get("min_share_info").toString());
				if(memberVO!=null){
					String ticket_rule = configDao.loadConfig("MEMBER_PRICE_TICKET");
					String percent_rule = configDao.loadConfig("MEMBER_PRICE_PERCENT");
					//满足条件
					if(Integer.parseInt(standardvo.getTicket()) >= Integer.parseInt(ticket_rule) || Integer.parseInt(standardvo.getTicket()) / (Double.parseDouble(standardvo.getSell_price()) * 100) >= Double.parseDouble(percent_rule)){
						if(1 == member_rate){
							standardvo.setMember_price(NumberUtil.formatDouble(Double.parseDouble(standardvo.getTicket_price())+Integer.parseInt(standardvo.getTicket())*0.55,2));
							standardvo.setReduce_money(NumberUtil.formatDouble(Integer.parseInt(standardvo.getTicket())*0.55, 2));	
						}else{
							standardvo.setMember_price(NumberUtil.formatDouble(Double.parseDouble(standardvo.getTicket_price())+Integer.parseInt(standardvo.getTicket())*member_rate,2));
							standardvo.setReduce_money(NumberUtil.formatDouble(Integer.parseInt(standardvo.getTicket())*member_rate, 2));	
						}
					}else{
						standardvo.setMember_price("");
						standardvo.setReduce_money("");
					}
				}else{
					String ticket_rule = configDao.loadConfig("MEMBER_PRICE_TICKET");
					String percent_rule = configDao.loadConfig("MEMBER_PRICE_PERCENT");
					if(Integer.parseInt(standardvo.getTicket()) >= Integer.parseInt(ticket_rule) || Integer.parseInt(standardvo.getTicket()) / (Double.parseDouble(standardvo.getSell_price()) * 100) >= Double.parseDouble(percent_rule)){
						standardvo.setMember_price(NumberUtil.formatDouble(Double.parseDouble(standardvo.getTicket_price())+Integer.parseInt(standardvo.getTicket())*0.55,2));
						standardvo.setReduce_money(NumberUtil.formatDouble(Integer.parseInt(standardvo.getTicket())*0.55, 2));		
					}else{
						standardvo.setMember_price("");
						standardvo.setReduce_money("");
					}
				}
				List<String> tpyeIdNocontainLast = new ArrayList<String>();
				List<String> tpyeNameNocontainLast = new ArrayList<String>();
				for (int i = 0; i < type_id_list.length; i++) {
					if(!type_id_list[i].contains(last_type_id)){
						tpyeIdNocontainLast.add(type_id_list[i]);
					}
				}
				for (int i = 0; i < type_child_name_list.length; i++) {
					if(!type_child_name_list[i].contains(last_type_child_name)){
						tpyeNameNocontainLast.add(type_child_name_list[i]);
					}
				}
			
				Map<String,Object> map3 = new HashMap<String,Object>();
				map3.put("good_id", good_id);
				map3.put("type_id1", last_type_id);
				map3.put("type_id2", tpyeIdNocontainLast.get(0));
				map3.put("type_child_name1", last_type_child_name);
				map3.put("type_child_name2", tpyeNameNocontainLast.get(0));
				map3.put("count", 2);
			
				Map<String,Object> map4 = new HashMap<String,Object>();
				map4.put("good_id", good_id);
				map4.put("type_id1", last_type_id);
				map4.put("type_id2", tpyeIdNocontainLast.get(1));
				map4.put("type_child_name1", last_type_child_name);
				map4.put("type_child_name2", tpyeNameNocontainLast.get(1));
				map4.put("count", 2);
				
				List<StandardVO> standardList = new ArrayList<StandardVO>();
				StandardVO standardType1 =  goodVOMapper.getOneType1(map3);
				List<StandardVO> chidList1 =  goodVOMapper.getPartOfDetails1(map3);
				StandardVO retObj1 = new StandardVO();
				retObj1.setChildList(chidList1);
				retObj1.setType_id(standardType1.getType_id());
				retObj1.setType_name(standardType1.getType_name());
				retObj1.setGood_id(standardType1.getGood_id());
				retObj1.setStandard_msg(standardvo);
				
				StandardVO standardType2 =  goodVOMapper.getOneType1(map4);
				List<StandardVO> chidList2 =  goodVOMapper.getPartOfDetails1(map4);
				StandardVO retObj2 = new StandardVO();
				retObj2.setChildList(chidList2);
				retObj2.setType_id(standardType2.getType_id());
				retObj2.setType_name(standardType2.getType_name());
				retObj2.setGood_id(standardType2.getGood_id());
				retObj2.setStandard_msg(standardvo);			
				standardList.add(retObj1);
				standardList.add(retObj2); 
				return standardList;
			}
			
		}
		return "";
	}

	public void updataShareTimes(GoodVO goodVO) {
		goodVOMapper.updataShareTimes(goodVO);
	}
  
	public Map getStandardPriAndShare(String standard_id,MemberVO memberVO){
		Map map  =  new HashMap();
		GoodVO good = goodVOMapper.getGoodByStandardId(standard_id);
		//优惠购
		//String buy_type,String ticket,String ticket_price, MemberVO memberVO
		try{
			String privilege_price = processPrivilegePrice(good.getBuy_type(),good.getTicket(),good.getTicket_price(),memberVO,configDao.loadConfig("SHORT_TICKET_NAME"));
			map.put("privilege_price", privilege_price);
			String member_price = processMemberPrice(good.getSell_price(), good.getBuy_type(),good.getTicket(),good.getTicket_price(),memberVO);
			map.put("member_price", member_price);
			if(memberVO!=null){
				if(memberVO.getId()!=null){
					int level_num = materialService.checkGtLevel(memberVO.getId(),PropertieUtil.loadPropertie("vip_level"));
					if(level_num > 0){
						Map shareInfomap = materialService.newProcessShareInfo(good.getJoin_award(), good.getJoin_award_money(), good.getBuy_type(), good.getTicket());
						map.put("share_info", shareInfomap.get("share_info").toString());
						map.put("min_share_info", shareInfomap.get("min_share_info").toString());
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}
	
	public List<OrderGoodVO> getOrderGoodList() {
		return goodVOMapper.getOrderGoodList();
	}

	public void updOrderGoodImg(String id, String file_name) {
		goodVOMapper.updOrderGoodImg(id,file_name);
	}

	public void updTest() {
		orderService.updTest();
		timerService.updTest();
	}
	
	@Override
	public String getSaveInfo(MemberVO memberVO) throws Exception {
		String saveInfo = "";
		if(memberVO==null || memberVO.getLevel()==null || PropertieUtil.loadPropertie("common_level").equals(memberVO.getLevel())){
			saveInfo = "加入会员，一年可省3865元";
		}
		return saveInfo;
	}

	@Override
	public MemberVO updataDeliveryAddress(String member_id, String address_id, String city_id) {
		MemberVO city_info=new MemberVO();
		String address="";
		if(!"".equals(address_id)&&address_id!=null){
			city_id = memberDao.selectCityId(address_id);
			address =memberDao.getMemberAddress(address_id);
		}
		//先更新默认地址（广州市天河区）
		memberDao.updataDefaultDeliveryAddress(member_id);
		//再更新选择地址
		memberDao.updataDeliveryAddress(city_id,address_id,member_id);
		
		MemberVO vo=memberDao.getCityParentCity(city_id);
		city_info=memberCartServise.packageAddress(vo);
		
		if(city_info.getCity_name2().equals(city_info.getCity_name1())){
			city_info.setCity_level("2");
		}else{
			city_info.setCity_level("3");
		}
		city_info.setCart_address(address);
		city_info.setAddress_id(address_id);
		return city_info;
	}

	@Override
	public Map getGoodsFreightAndDeliveryAddressState(String member_id, String address_id, String city_id,
			String good_id, String num) {
		Map map = new HashMap();
		
		//distribution_status 0 配送 1 不支持配送
		int distribution_status=1;
		//运费  freight_type 1 买家付运费
		double freight=0;
		if(!"".equals(address_id)&& address_id !=null){
			city_id=memberDao.getCityId(address_id);
		}
		GoodVO goodVO = goodVOMapper.selectgood(good_id);
		String standard_id =goodVOMapper.getGoodStandardId(good_id);
		if(goodVO.getSend_temp_id()!=null){
			
			List<SendTempItemVO> list = getSendTempItemBySendTempId(goodVO.getSend_temp_id(),city_id);
			
			if(list.size()>0){
				if("1".equals(goodVO.getFreight_type())){	
					freight=goodTempService.getFreightByCity(standard_id.split(","),num.split(","),city_id);
				}
				distribution_status = 0;
			}

		}else{
			distribution_status = 0;
			if("1".equals(goodVO.getFreight_type())){
				freight=goodTempService.getFreightByCity(standard_id.split(","),num.split(","),city_id);
			}
		}
		DecimalFormat    df   = new DecimalFormat("######0.00");   

		map.put("distribution_status",distribution_status+"");
		map.put("freight",df.format(freight));
		map.put("freight_type",goodVO.getFreight_type());
		
		return map;
	}
	/*
	 * @see service.v2.GoodService#getAddressDefaultInfo(java.lang.String)
	 */
	@Override
	public MemberVO getAddressDefaultInfo(String member_id,String good_id, String lang) {
		AddressVO address = new AddressVO();
		MemberVO city_info=new MemberVO();
		String city_id=null;
		Map map = new HashMap();
		if(lang==null){
			if(member_id==null || "".equals(member_id)){
				city_info = memberCartServise.getDefaultaAddress();
			}else{
				city_info=memberCartServise.getCartCityInfo(member_id);
			}
			map = getGoodsFreightAndDeliveryAddressState(member_id,city_info.getAddress_id(),city_info.getCity_id3(),good_id,"1");
		}else{
			if(member_id==null || "".equals(member_id)){
				city_info = memberCartServise.getDefaultaAddressInter("51081",lang);
			}else{
				city_info=memberCartServise.getCartCityInfoInter(member_id,lang);
			}
			if(city_info.getCity_id2()==null){
				city_id=city_info.getCity_id1();
			}
			if(city_id==null){
				city_id=city_info.getCity_id0();
			}
			map = getGoodsFreightAndDeliveryAddressStateEn(member_id,city_info.getAddress_id(),city_id,good_id,"1",city_id);
		}
		
		//getDefaultaAddressInter
		city_info.setDistribution_status((String)map.get("distribution_status"));
		city_info.setFreight(map.get("freight"));
		city_info.setFreight_type((String)map.get("freight_type"));
		
		return city_info;
	}

	@Override
	public List<SendTempItemVO> getSendTempItemBySendTempId(String send_temp_id, String city_id) {
		List<SendTempItemVO> list = goodVOMapper.getSendTempItemBySendTempId(send_temp_id,city_id);
		return list;
	}
	@Override
	public GoodDeliveryInterCountryVO getSendTempItemBySendTempIdEn(String good_temp_id, String country_id) {
		GoodDeliveryInterCountryVO vo = goodVOMapper.getSendTempItemBySendTempIdEn(good_temp_id,country_id);
		return vo;
	}

	@Override
	public List<GoodVO> getOrdinaryMemberGoodList(GoodVO goodVO) {
		return goodVOMapper.getOrdinaryMemberGoodList(goodVO);
	}

	@Override
	public List<GoodVO> getGoodListT(GoodVO goodVO) {
		return goodVOMapper.getGoodListT(goodVO);
	}

	@Override
	public List<GoodVO> tempDefaultGoodList(String member_id, String screenwidth,String lang) throws Exception {
		GoodVO goodVO =new GoodVO();
		List<GoodVO>goodList_d=new ArrayList<GoodVO>();
		if(lang==null){
			goodVO.setCat_id(PropertieUtil.loadPropertie("member_default_cat"));
		}else{
			goodVO.setCat_id(PropertieUtil.loadPropertie("inter_member_default_cat"));
		}
		if(PropertieUtil.loadPropertie("package_cat").equals(goodVO.getCat_id())){
			goodVO.setPackage_yn("Y");
		}
		
		MemberVO memberVO=memberDao.getAllInfoById(member_id);
		int total = goodVOMapper.getOrdinaryMemberGoodListCount(goodVO);
		int page_count = 3;
		int page_size = total/page_count;
		if(page_size*page_count<total){
			page_size=page_size+1;
		}
		goodVO.setPage_count("3");
		int limitfrom=0;
		for(int i =0; i<page_size;i++){
			GoodVO vo =new GoodVO();
			List<GoodVO> list = goodVOMapper.getOrdinaryGoodList(goodVO,limitfrom,page_count);
		
			///图片缩放
			if(screenwidth != null && StringUtil.isNumeric(screenwidth)&&list.get(0).getImg().indexOf("x-oss-process")==-1){
				processImg(list, screenwidth,3);
			}
			if(!PropertieUtil.loadPropertie("package_cat").equals(goodVO.getCat_id())){
				list = processGoodList(list, memberVO, "券");
			}
			List<GoodVO> list1 = packageGoodPrice(list);
			vo.setGoodList_p(list1);
			goodList_d.add(vo);
			limitfrom=limitfrom+3;
		}
		
		return goodList_d;
	}

	@Override
	public List<GoodVO> packageGoodPrice(List<GoodVO> goodList) {
		
		List<GoodVO> goodList_d=goodList;
		
		for(int i=0;i<goodList_d.size();i++){
			
			if(goodList_d.get(i).getMember_price()!=null){
				String m =goodList_d.get(i).getMember_price().trim();
				
				if(m.indexOf(".00") > 0){
					//正则表达
					m = m.replaceAll("0+?$", "");//去掉后面无用的零
					m = m.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
				}
				goodList_d.get(i).setMember_price(m);
			}
			if(goodList_d.get(i).getDefault_price()!=null){
				String d =goodList_d.get(i).getDefault_price().trim();
				
				if(d.indexOf(".00") > 0){
					  //正则表达
					  d = d.replaceAll("0+?$", "");//去掉后面无用的零
					  d = d.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
				}
				goodList_d.get(i).setDefault_price(d);
			}
			if(goodList_d.get(i).getMarket_price()!=null){
				String d =goodList_d.get(i).getMarket_price().trim();
				
				if(d.indexOf(".00") > 0){
					//正则表达
					d = d.replaceAll("0+?$", "");//去掉后面无用的零
					d = d.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
				}
				goodList_d.get(i).setMarket_price(d);
			}
			
		}
		return goodList_d;
	}

	@Override
	public String checkCountry(String standard_id) {
		return goodVOMapper.checkCountry(standard_id);
	}

	@Cacheable(value="GoodServiceImpl_getHotWatchByGoodId",key="#root.targetClass+#root.methodName+#id")
	public List<GoodVO> getHotWatchByGoodId(String id) {
		return goodVOMapper.getHotWatchByGoodId(id);
	}
	@Override
	public MemberVO updataDeliveryAddressEn(String member_id, String address_id, String city_id) {
		MemberVO city_info=new MemberVO();
		String address="";
		String apt="";
		if(!"".equals(address_id)&&address_id!=null){
			AddressVO addressVO = addressDao.getAddressByIdEn(address_id);
			city_id =addressVO.getCountry_id();
			if(addressVO.getProvince_id() != null){
				city_id = addressVO.getProvince_id();
			}
			if(addressVO.getCity_id() != null){
				city_id = addressVO.getCity_id();
			}
			address =addressVO.getAddress();
			apt=addressVO.getApt();
		}
		//先更新默认地址（Alhambra, California, United states）
		memberDao.updataDefaultDeliveryAddressEn(member_id);//--------------------
		//再更新选择地址
		memberDao.updataDeliveryAddressEn(city_id,address_id,member_id);
		
		MemberVO vo=memberDao.getCityParentCityEn(city_id);
		city_info=memberCartServise.packageAddressEn(vo);
		
		city_info.setCart_address(address);
		city_info.setAddress_id(address_id);
		city_info.setApt(apt);
		return city_info;
	}
	@Override
	public Map getGoodsFreightAndDeliveryAddressStateEn(String member_id, String address_id, String city_id,
			String good_id, String num,String country_id) {
		Map map = new HashMap();
		
		//distribution_status 0 配送 1 不支持配送
		int distribution_status=1;
		//运费  freight_type 1 买家付运费
		double freight=0;
		if(!"".equals(address_id)&& address_id !=null){
			AddressVO addressVO = addressDao.getAddressByIdEn(address_id);
			city_id =addressVO.getCountry_id();
			if(addressVO.getProvince_id() != null){
				city_id = addressVO.getProvince_id();
			}
			if(addressVO.getCity_id() != null){
				city_id = addressVO.getCity_id();
			}
			country_id=addressVO.getCountry_id();
		}
		GoodVO goodVO = goodVOMapper.selectgood(good_id);
		String standard_id =goodVOMapper.getGoodStandardId(good_id);
		if(goodVO.getGood_temp_id()!=null){
			
			GoodDeliveryInterCountryVO vo = getSendTempItemBySendTempIdEn(goodVO.getGood_temp_id(),country_id);
			
			if(vo !=null){
				if("1".equals(goodVO.getFreight_type())){	
					freight=goodTempService.getFreightByCityEn(standard_id.split(","),num.split(","),city_id);
				}
				distribution_status = 0;
			}

		}else{
			distribution_status = 0;
			if("1".equals(goodVO.getFreight_type())){
				freight=goodTempService.getFreightByCityEn(standard_id.split(","),num.split(","),city_id);
			}
		}
		DecimalFormat df = new DecimalFormat("######0.00");   

		map.put("distribution_status",distribution_status+"");
		map.put("freight",df.format(freight));
		map.put("freight_type",goodVO.getFreight_type());
		
		return map;
	}
}
